package com.gouqi.mall.service.impl;

import com.gouqi.mall.domain.Enterprise;
import com.gouqi.mall.domain.EnterpriseProduct;
import com.gouqi.mall.domain.ResultJson;
import com.gouqi.mall.mapper.EnterpriseProductMapper;
import com.gouqi.mall.service.EnterpriseProductService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EnterpriseProductServiceImpl implements EnterpriseProductService {

    @Autowired
    private EnterpriseProductMapper mapper;
    private JSONObject json = new JSONObject();
    private ResultJson result = new ResultJson();
    @Override
    public String addProduction(EnterpriseProduct product) {
        Integer id = mapper.addProduction(product);
        if (id > 0) {
            result.setStatus(200);
            result.setCode(200);
            result.setMessage("新增企业产品信息成功！");
            result.setResultJson(id);
        } else {
            result.setStatus(404);
            result.setCode(204);
            result.setMessage("新增企业产品信息失败！");
            result.setResultJson("");
        }
        json.put("result",result);
        return json.toString();
    }

    @Override
    public String updateProduction(EnterpriseProduct product) {
        Integer id = mapper.updateProduction(product);
        if (id > 0) {
            result.setStatus(200);
            result.setCode(200);
            result.setMessage("更新企业产品信息成功！");
        } else {
            result.setStatus(404);
            result.setCode(204);
            result.setMessage("更新企业产品信息失败！");
        }
        json.put("result",result);
        return json.toString();
    }

    @Override
    public String deleteProduction(Integer id) {
        Integer res = mapper.deleteProduction(id);
        if (res > 0) {
            result.setStatus(200);
            result.setCode(200);
            result.setMessage("删除一家企业信息成功！");
            result.setResultJson(null);
        } else {
            result.setStatus(404);
            result.setCode(404);
            result.setMessage("删除一家企业信息失败！");
        }
        json.put("result",result);
        return json.toString();
    }

    @Override
    public String queryProduction(String name) {
        List<EnterpriseProduct> enterprise= mapper.queryProduction(name);
        if (enterprise != null) {
            result.setStatus(200);
            result.setCode(200);
            result.setMessage("查找一家企业信息成功！");
            result.setResultJson(enterprise);
        } else {
            result.setStatus(404);
            result.setCode(404);
            result.setMessage("查找一家企业信息失败！");
            result.setResultJson(null);
        }
        json.put("result",result);
        return json.toString();
    }
}
